# -*- coding: utf-8 -*-
# @Time    : 2023/2/9 16:26
# @Author  : kali
from models import Question


def get_question_data(req_data):
    """
    查询问题数据
    :param req_data:
    :return:
    """
    data = []
    count = 0
    try:
        page_size = int(req_data.get("page_size", 10))
        page_num = int(req_data.get("page_num", 1))
        query = req_data.get("query")
        if query:
            objs = Question.query.filter(Question.tag.like(query))
        else:
            objs = Question.query.filter()
        count = objs.count()
        objs = objs.order_by(Question.votes.desc()).paginate(page=page_num, per_page=page_size)

        for row in objs:
            data.append(row.to_dict())

        print(data)
    except Exception as e:
        import traceback
        traceback.print_exc()
        print(f"get_question_data error,msg:{str(e)}")
    return data, count
